package org.darkforest.ucenter.service.impl;

import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.FestivalMapper;
import org.darkforest.dao.mapper.FestivalRemindMapper;
import org.darkforest.dao.mapper.MemberFriendsMapper;
import org.darkforest.dao.mapper.RelationMapper;
import org.darkforest.model.Festival;
import org.darkforest.model.FestivalRemind;
import org.darkforest.model.MemberFriends;
import org.darkforest.ucenter.service.ContactsService;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2017/4/21.
 */
@Controller
public class ContactsServiceImpl implements ContactsService {
    @Autowired
    private MemberFriendsMapper memberFriendsMapper;
    @Autowired
    private FestivalRemindMapper festivalRemindMapper;
    @Autowired
    private FestivalMapper festivalMapper;

    /****
     * 添加联系人
     * @param record
     * @param memberBase
     */
    @Override
    @Transactional
    public void addfriends(MemberFriends record, MemberBase memberBase) {
        record.setCreaterId(memberBase.getUserId());
        record.setCreateName(memberBase.getUserName());
        record.setCreateTime(DateUtil.toTimeString(new Date()));
        memberFriendsMapper.insert(record);
        if(record.getBirthday() != null && !record.getBirthday().equals("")){
            FestivalRemind festivalRemind = new FestivalRemind();
            festivalRemind.setfRelationMemberIds(String.valueOf(record.getfId()));
            festivalRemind.setfRemindType("1");
            festivalRemind.setfRemindName("生日");
            festivalRemind.setfDate(record.getBirthday());
            festivalRemind.setShowFlag(DateUtil.getReminaShowFlag(record.getBirthday()));
            festivalRemind.setCreaterId(memberBase.getUserId());
            festivalRemind.setCreateName(memberBase.getUserName());
            festivalRemind.setCreateTime(DateUtil.toTimeString(new Date()));
            festivalRemind.setIsGift("2");
            festivalRemindMapper.insert(festivalRemind);
        }
    }

    /****
     *编辑联系人
     * @param record
     * @param memberBase
     */
    @Override
    @Transactional
    public void editfriends(MemberFriends record, MemberBase memberBase) {
        record.setCreateTime(DateUtil.toTimeString(new Date()));
        memberFriendsMapper.updateByPrimaryKeySelective(record);
        if(record.getBirthday()!=null&& !record.getBirthday().equals("")){
            FestivalRemind festivalRemind = new FestivalRemind();
            festivalRemind.setfRelationMemberIds(String.valueOf(record.getfId()));
            festivalRemind.setfRemindType("1");
            List<FestivalRemind> list = festivalRemindMapper.selectListByCondition(festivalRemind);
            if(list.size()>0&&list.get(0)!=null){
                festivalRemind.setfId(list.get(0).getfId());
                festivalRemind.setfDate(DateUtil.getRemindDate(record.getBirthday()));
                festivalRemind.setShowFlag(DateUtil.getReminaShowFlag(festivalRemind.getfDate()));
                festivalRemindMapper.updateByPrimaryKeySelective(festivalRemind);
            }else{
                festivalRemind.setfRemindName("生日");
                festivalRemind.setfDate(DateUtil.getRemindDate(record.getBirthday()));
                festivalRemind.setShowFlag(DateUtil.getReminaShowFlag(festivalRemind.getfDate()));
                festivalRemind.setCreaterId(memberBase.getUserId());
                festivalRemind.setCreateName(memberBase.getUserName());
                festivalRemind.setCreateTime(DateUtil.toTimeString(new Date()));
                festivalRemind.setIsGift("2");
                festivalRemindMapper.insert(festivalRemind);
            }
        }
    }

    /****
     * 删除联系人
     * @param fId
     */
    @Override
    @Transactional
    public void delfriends(Integer fId) {
        memberFriendsMapper.deleteByPrimaryKey(fId);
        festivalRemindMapper.deleteByfriendsId(String.valueOf(fId));
    }

    /*****
     * 添加纪念日
     * @param record
     * @param memberBase
     */
    @Override
    @Transactional
    public void addanniversary(FestivalRemind record, MemberBase memberBase) {
        MemberFriends mf = new MemberFriends();
        mf.setfName(record.getfName());
        mf.setCreaterId(memberBase.getUserId());
        List<MemberFriends>  list = memberFriendsMapper.selectByCondition(mf);
        if(list.size()<1){
            mf.setCreateName(memberBase.getUserName());
            mf.setCreateTime(DateUtil.toTimeString(new Date()));
            memberFriendsMapper.insert(mf);

            record.setfRelationMemberIds(mf.getfId().toString());
            record.setfDate(DateUtil.getRemindDate(record.getfDate()));
            record.setShowFlag(DateUtil.getReminaShowFlag(record.getfDate()));
            record.setCreaterId(memberBase.getUserId());
            record.setCreateName(memberBase.getUserName());
            record.setCreateTime(DateUtil.toTimeString(new Date()));
            record.setIsGift("2");
            record.setfRemindType("2");
            record.setfRemindState(1);

            festivalRemindMapper.insert(record);
        }else if (list.size()==1){
            mf = list.get(0);
            record.setfRelationMemberIds(mf.getfId().toString());
            record.setfDate(DateUtil.getRemindDate(record.getfDate()));
            record.setShowFlag(DateUtil.getReminaShowFlag(record.getfDate()));
            record.setCreaterId(memberBase.getUserId());
            record.setCreateName(memberBase.getUserName());
            record.setCreateTime(DateUtil.toTimeString(new Date()));
            record.setIsGift("2");
            record.setfRemindType("2");
            record.setfRemindState(1);

            festivalRemindMapper.insert(record);
        }else{
            System.out.print("重复");
        }

    }

    /***
     * 全年送 添加大众节日或者自定义节日
     * @param memberFriendsId
     * @param form_type
     * @param ids
     * @param record
     * @param memberBase
     */
    @Override
    @Transactional
    public void addgiftfestival(Integer memberFriendsId, String form_type, String ids, Festival record, MemberBase memberBase) {
        FestivalRemind festivalRemind = new FestivalRemind();
        if(form_type!=null&&form_type.equals("1")){
            String[] strs = ids.split(",");
            if(strs.length>0){
                for(String id :strs){
                    Festival festival = festivalMapper.selectByPrimaryKey(Integer.parseInt(id));
                    festivalRemind.setfRelationMemberIds(String.valueOf(memberFriendsId));
                    festivalRemind.setfRemindName(festival.getfName());
                    festivalRemind.setfDate(DateUtil.getRemindDate(festival.getfDate()));
                    List<FestivalRemind> list = festivalRemindMapper.selectListByCondition(festivalRemind);
                    if(list.size()==0){
                        festivalRemind.setShowFlag(DateUtil.getReminaShowFlag(record.getfDate()));
                        festivalRemind.setCreaterId(memberBase.getUserId());
                        festivalRemind.setCreateName(memberBase.getUserName());
                        festivalRemind.setCreateTime(DateUtil.toTimeString(new Date()));
                        festivalRemind.setIsGift("2");
                        festivalRemind.setfRemindType("3");
                        festivalRemindMapper.insert(festivalRemind);
                    }
                }
            }
        }else{
            festivalRemind.setfRemindName(record.getfName());
            festivalRemind.setfRelationMemberIds(String.valueOf(memberFriendsId));
            festivalRemind.setfDate(DateUtil.getRemindDate(record.getfDate()));
            festivalRemind.setShowFlag(DateUtil.getReminaShowFlag(festivalRemind.getfDate()));
            festivalRemind.setCreaterId(memberBase.getUserId());
            festivalRemind.setCreateName(memberBase.getUserName());
            festivalRemind.setCreateTime(DateUtil.toTimeString(new Date()));
            festivalRemind.setIsGift("2");
            festivalRemind.setfRemindType("2");
            festivalRemindMapper.insert(festivalRemind);
        }
    }
}
